package com.lolaage.tracks.adapter;

import android.location.Location;

import com.lolaage.tracks.MyLocationValider;


/*********************************************************
 * @author : zhaohua
 * @version : 2012-9-19
 * @see : 最新位置的合法判断者
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class LastLocationValider implements MyLocationValider
{
    /** 有效时间差值 */
    protected int VALID_TIME = 1000 * 60 * 2;
    /** 有效精度差值 */
    protected int VALID_ACCACURY = 50;
    /** 最大的精度值*/
    protected int MAX_REQ_ACCACURY = 100;
    /** 最大有效距离 */
    protected int MAX_DISTANCE = 20;
    
    @Override
    public boolean isBetterLocation(Location currentBestLocation, Location location)
    {
        if (location == null) { return false; }
        if (currentBestLocation == null || currentBestLocation.getProvider().equals("init"))
        {
            // A new location is always better than no location
            return true;
        }
        
        // Check whether the new location fix is newer or older
        long timeDelta = location.getTime() - currentBestLocation.getTime();
        boolean isSignificantlyNewer = timeDelta > VALID_TIME;
        boolean isSignificantlyOlder = timeDelta < -VALID_TIME;
        boolean isNewer = timeDelta > 0;
        // If it's been more than two minutes since the current location, use
        // the new location
        // because the user has likely moved
        if (isSignificantlyNewer)
        {
            return true;
        } else
        // If the new location is more than two minutes older, it must be worse
        if (isSignificantlyOlder)
        { 
            return false;
        }
        
        // Check whether the new location fix is more or less accurate
        if(location.getAccuracy() > MAX_REQ_ACCACURY)
        {
            return false;
        }
        int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
        boolean isLessAccurate = accuracyDelta > 0;
        boolean isMoreAccurate = accuracyDelta < 0;
        boolean isSignificantlyLessAccurate = accuracyDelta > VALID_ACCACURY;
        // Check if the old and new location are from the same provider
        boolean isFromSameProvider = isSameProvider(location.getProvider(),currentBestLocation.getProvider());
        // Determine location quality using a combination of timeliness and accuracy
        if (isMoreAccurate)
        {
            return true;
        } else 
        if (isNewer && !isLessAccurate)
        {
            return true;
        } else 
        if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) 
        {
            return true;
        }
        
        // check distance 
        int distDetal = (int)location.distanceTo(currentBestLocation);
        if( isNewer && distDetal <= MAX_DISTANCE)
        {
            return true;
        }
        
        return false;
    }
    
    protected boolean isSameProvider(String provider1, String provider2)
    {
        if (provider1 == null) { return provider2 == null; }
        return provider1.equals(provider2);
    }
}
